/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aaron.platform.service;

import com.aaron.platform.dao.IEnterpriseDao;
import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.FgysEnterprise;
import com.aaron.platform.entity.GczxPaymentInfo;
import com.aaron.platform.exceptions.EnterpriseActiveException;
import com.aaron.platform.exceptions.EnterpriseAllreadyExistException;
import com.aaron.platform.exceptions.EnterpriseNotExistException;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.springframework.dao.DataAccessException;

/**
 *
 * @author Administrator
 */
public class EnterPriseMgrImp implements  IEnterpriseMgr {

    private IEnterpriseDao enterpriseDao;
    
    private IPaymentMgr paymentMgr;

    public void setEnterpriseDao(IEnterpriseDao enterpriseDao) {
        this.enterpriseDao = enterpriseDao;
    }

    public void setPaymentMgr(IPaymentMgr paymentMgr) {
        this.paymentMgr = paymentMgr;
    }

    public FgysEnterprise findEnterpriseById(Integer enterpriseId) throws EnterpriseNotExistException {
        return enterpriseDao.findById(enterpriseId);
    }

    public FgysEnterprise findEnterpriseByName(String enterpriseName) throws EnterpriseNotExistException {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(FgysEnterprise.class);
        detachedCriteria = detachedCriteria.add(Expression.eq(FgysEnterprise.ENTERPRISE_NAME, enterpriseName));
        List<FgysEnterprise> enterprises = enterpriseDao.findByCriteria(detachedCriteria);
        if (enterprises != null && enterprises.size() > 0) {
            return enterprises.get(0);
        } else {
            return null;
        }
    }

    public List<FgysEnterprise> findAllEnterprises() {
        return enterpriseDao.FindAll();
    }

    public void addEnterprise(FgysEnterprise fgysEnterprise) throws EnterpriseAllreadyExistException {
        // 单位是否已存在
        FgysEnterprise ernterprise = null;
        try {
            ernterprise = findEnterpriseByName(fgysEnterprise.getEnterpriseName());
        } catch (EnterpriseNotExistException ex) {
            Logger.getLogger(EnterPriseMgrImp.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ernterprise != null) {
            throw new EnterpriseAllreadyExistException(fgysEnterprise.getEnterpriseName());
        }
        enterpriseDao.save(fgysEnterprise);
    }

    public void updateEnterprise(FgysEnterprise fgysEnterprise) throws EnterpriseAllreadyExistException, EnterpriseNotExistException {
        
        // 不允许修改成和其他单位名称一样
        FgysEnterprise enterprise = findEnterpriseByName(fgysEnterprise.getEnterpriseName());
        if (enterprise != null && !fgysEnterprise.equals(enterprise)){
            throw new EnterpriseAllreadyExistException(fgysEnterprise.getEnterpriseName());
        }
        try{
            enterpriseDao.merge(fgysEnterprise);
        }
        catch(DataAccessException dae){
            // tranlate the exception for the user has already been removed
            enterprise = enterpriseDao.findById(fgysEnterprise.getEnterpriseId());
            if (enterprise == null){
                throw new EnterpriseNotExistException();
            }
            // throw the exception for other conditions
            throw dae;
        }
        
    }

    public void removeEnterprise(Integer enterpriseId) throws EnterpriseActiveException {
        
        // 若该合作单位下有付款信息，不允许删除
        Properties condition = new Properties();
        condition.setProperty(GczxPaymentInfo.ENTERPRISE_ID, String.valueOf(enterpriseId));
        PageInfo<GczxPaymentInfo> pageInfo = paymentMgr.findByCondition(condition, 0, 10);
        if (pageInfo != null && pageInfo.getTotalCount() > 0) {
            throw new EnterpriseActiveException();
        }
        // 删除
        enterpriseDao.removeById(enterpriseId);
    }

    public PageInfo findEnterprisesByCondition(Properties condition, int startIndex, int pageSize) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(FgysEnterprise.class);
        if (condition != null && !condition.isEmpty()) {
            String enterpriseName = condition.getProperty(FgysEnterprise.ENTERPRISE_NAME);
           if (!StringUtils.isEmpty(enterpriseName)) {
                detachedCriteria = detachedCriteria.add(Expression.ilike(FgysEnterprise.ENTERPRISE_NAME,
                        enterpriseName, MatchMode.ANYWHERE));
           }
        }
        return enterpriseDao.findPageByCriteria(detachedCriteria,  startIndex, pageSize);
    }

}
